//
//  SelectContactViewController.m
//  Edumap
//
//  Created by 辰领科技 on 16/7/20.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "SelectContactViewController.h"
#import "SelectContactTableViewCell.h"
#import "EditContactViewController.h"
#import "MakeSureTheOrderViewController.h"

static NSString *cellIdentifier = @"SelectContactTableViewCell";

@interface SelectContactViewController ()
<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray * addressArray;


@end

@implementation SelectContactViewController

- (void)viewWillAppear:(BOOL)animated{
    [self getAddressList];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _addressArray = [NSMutableArray array];
    [self initViewLayout];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    
    [_tableView registerNib:[UINib nibWithNibName:cellIdentifier bundle:nil] forCellReuseIdentifier:cellIdentifier];
    _tableView.dataSource = self;
    _tableView.delegate = self;
}


- (IBAction)back:(id)sender {
    [self backBtn];
}

#pragma mark - 添加
- (IBAction)add:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeTeachStoryboard" bundle:nil];
    EditContactViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"EditContact"];
    
    page.isEditContact = NO;
    
    [[Common getNavigation] pushViewController:page];
}

#pragma mark === 获取地址列表
- (void)getAddressList{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"msadMemberId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_queryMallShippingAddress withHttpType:@"POST" withTag:JY_JY_queryMallShippingAddress];
}

#pragma mark - <UITableViewDelegate&&UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//定义 UITableView cell 的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _addressArray.count;
}

//定义 UITableView cell 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 132*VerticalRatio();
}

//定义 UITableView cell 展示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SelectContactTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell= [[SelectContactTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    cell.BGImage.image = [UIImage imageNamed:@"SelectContact4"];
    [cell.button setImage:[UIImage imageNamed:@"SelectContact2"] forState:UIControlStateNormal];
    if (_count == indexPath.row) {
        cell.BGImage.image = [UIImage imageNamed:@"SelectContact3"];
        [cell.button setImage:[UIImage imageNamed:@"SelectContact1"] forState:UIControlStateNormal];
    }
    NSDictionary * dic= _addressArray[indexPath.row];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@",dic[@"msadReceiverName"]];
    cell.phoneNumberLabel.text = [NSString stringWithFormat:@"%@",dic[@"msadMobileNo"]];
    cell.addressLabel.frame = flexibleFrame(CGRectMake(99, 76, 193, 0), NO);
    cell.addressLabel.numberOfLines = 0;
    cell.addressLabel.text = [NSString stringWithFormat:@"%@%@%@%@",dic[@"msadProvinceName"],dic[@"msadCityName"],dic[@"msadAreaName"],dic[@"msadAddr"]];
    [cell.addressLabel sizeToFit];
    
    cell.block = ^(){
        UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeTeachStoryboard" bundle:nil];
        EditContactViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"EditContact"];
        page.addressId = [NSString stringWithFormat:@"%@",dic[@"msadId"]];
        page.isEditContact = YES;
        
        [[Common getNavigation] pushViewController:page];
    };
    
    return cell;
}

//定义 UITableView cell 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _count = indexPath.row;
    [tableView reloadData];
    _SelectContactBlock(YES,self.count);
    _choseAddress(_addressArray[indexPath.row]);
    [self backBtn];

}

#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==JY_JY_queryMallShippingAddress) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {

            _addressArray = database[@"result"];
            [_tableView reloadData];
        }
    }
    
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}


@end
